package com.wisely.highlight_spring4.ch1.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


@Aspect //1
@Component //2
/**
 * Advice 执行顺序：
 * Aroud Advice start , Before Advice,正常执行方法, Around Advice end,After advice,After returning advice.
 * 如果有异常抛出，则Aroud Advice start , Before Advice,正常执行方法，after-throwing advice,After advice.
 * @类功能说明:   
 * @公司名称     :中企动力
 * @作者                ：Administrator 
 * @创建时间      ：2016年8月17日 下午1:58:55  
 * @版本                ：V1.0
 */
public class LogAspect {
	
	@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") //3
	public void annotationPointCut(){};
	
	@Before(value = "annotationPointCut() && args(dd) ")
	 public void beforeAnnotion(JoinPoint joinPoint,int dd){
		System.out.println("dd="+dd);
		Object [] args = joinPoint.getArgs();
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class); 
        System.out.println("注解拦截前 " + action.name()); //5
	 }
	
	  @After("annotationPointCut()") //4
	    public void after(JoinPoint joinPoint) {
		  	Object target = joinPoint.getTarget();
		  	Object [] args = joinPoint.getArgs();
	        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
	        Method method = signature.getMethod();
	        Action action = method.getAnnotation(Action.class); 
	        System.out.println("注解拦截 后" + action.name()); //5
	    }
	  
	  @AfterReturning(value="annotationPointCut()",returning="returnvalue")
	  public void afterReturning(String returnvalue) {
		  System.out.println("return value is "+returnvalue);
	  }
	  
	   @Before("execution(* com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") //6
	    public void before(JoinPoint joinPoint){
	        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
	        Method method = signature.getMethod();
	        System.out.println("常规方法拦截,"+method.getName());

	    }
	   
	    @Around("annotationPointCut()")  
	    public Object doAroundAdvice(ProceedingJoinPoint pjp) throws Throwable{  
	        System.out.println("进入环绕通知");
	        String param = System.getProperty("param");
	        Object object = null;
	        if(param == null){
	        	object = pjp.proceed();
	        }else{
	        	object = pjp.proceed(new Object[]{Integer.parseInt(param)});//执行该方法  
	        }
	        System.out.println("invoke return : "+object);
	        System.out.println("退出方法");  
	        return object;  
	    } 
	    
	    @AfterThrowing(value="annotationPointCut()",throwing="ex")
	    public void throwingHander(Exception ex){
	    	if(ex != null){
	    		ex.printStackTrace();
	    	}
	    }
	   
	  
	
}
